<template>
    <el-dialog :title="!immunityForm.id ? '新增' : '修改'" :visible.sync="dialogVisible">
        <el-form ref="immunityForm" :model="immunityForm" :rules="immunityRules" label-width="110px">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="免疫项目:" prop="project">
                        <el-input v-model="immunityForm.project" placeholder="免疫项目"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="免疫对象:" prop="object">
                        <el-select v-model="immunityForm.object" placeholder="免疫对象" style="width:100%">
                            <el-option v-for="item in epidemicPreventionTargets" :key="item.value"
                                       :label="item.label"
                                       :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="免疫条件:" prop="condition">
                        <el-select v-model="immunityForm.condition" placeholder="免疫条件" style="width:100%">
                            <el-option v-for="item in condition" :key="item.value"
                                       :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="条件值:" prop="date">
                        <el-input placeholder="条件值" v-model="immunityForm.date"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="提前提醒天数:" prop="days">
                        <el-input v-model="immunityForm.days" placeholder="提前提醒天数"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="是否启用:">
                        <el-checkbox v-model="immunityForm.checked"></el-checkbox>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="备注:">
                        <el-input type="textarea" v-model="immunityForm.notes" placeholder="备注"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('immunityForm')">确 定</el-button>
            </span>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            dialogVisible: false,
            immunityForm: {
                id: '',
                project: '',
                object: '',
                condition: '',
                date: '',
                days: '',
                checked: false,
                notes: ''
            },
            immunityRules: {
                project: [
                    {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                ],
                object: [
                    {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                ],
                condition: [
                    {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                ],
                date: [
                    {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                ],
                days: [
                    {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                ]
            },
            condition: [
                {
                    value: '日龄',
                    label: '日龄'
                },
                {
                    value: '妊娠',
                    label: '妊娠'
                },
                {
                    value: '普通免疫',
                    label: '普通免疫'
                },
            ],
            epidemicPreventionTargets: [
                {
                    value: '生长猪',
                    label: '生长猪'
                },
                {
                    value: '种猪',
                    label: '种猪'
                },
            ]
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (!this.immunityForm.id) {
                        delete this.immunityForm.id
                    }
                    this.$http[!this.immunityForm.id ? 'post' : 'put'](`/a`, this.immunityForm).then((res) => {
                        if (res.code !== 0) {
                            return this.$message.error(res.msg);
                        }
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        this.dialogVisible = false;
                        this.$emit('updateDialog')
                    })
                } else {
                    return false;
                }
            });
        },
    }
}
</script>


